#include "stdafx.h"
#include "PlayerAvatar.h"

PlayerAvatar::PlayerAvatar(Ogre::SceneNode *node, Ogre::SceneNode *pitchNode)
{
	setSceneNode(node);
	setPitchNode(pitchNode);

}
void PlayerAvatar::turn(float amount)
{
	if(m_node)
	{
		m_node->yaw(Ogre::Degree(amount));

	}
}
void PlayerAvatar::pitchCamera(float amount)
{
	if(m_pitchNode)
	{
		 m_pitchNode->pitch(Ogre::Degree(amount));
		 // Angle of rotation around the X-axis.
		 float pitchAngle = (2 * Ogre::Degree(Ogre::Math::ACos(this->m_pitchNode->getOrientation().w)).valueDegrees());
 
		 // Just to determine the sign of the angle we pick up above, the
		 // value itself does not interest us.
		 float pitchAngleSign = this->m_pitchNode->getOrientation().x;
		 // Limit the pitch between -90 degress and +90 degrees, Quake3-style.
		 if (pitchAngle > 90.0f)
		 {
			 if (pitchAngleSign > 0)
				 // Set orientation to 90 degrees on X-axis.
				 this->m_pitchNode->setOrientation(Ogre::Quaternion(Ogre::Math::Sqrt(0.5f),
																		Ogre::Math::Sqrt(0.5f), 0, 0));
			 else if (pitchAngleSign < 0)
				 // Sets orientation to -90 degrees on X-axis.
				 this->m_pitchNode->setOrientation(Ogre::Quaternion(Ogre::Math::Sqrt(0.5f),
																		-Ogre::Math::Sqrt(0.5f), 0, 0));
		 }

	}
}

void PlayerAvatar::setPitchNode(Ogre::SceneNode *pitchNode)
{
	m_pitchNode = pitchNode;
	//TODO delete old cameranode???
}